﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BaseballGame.Models;
using System.Web.Security;
using BaseballGame.Filters;
using WebMatrix.WebData;
using Microsoft.Web.WebPages.OAuth;
using DotNetOpenAuth.AspNet;
using System.Transactions;
using BaseballGame.ViewModel;

namespace BaseballGame.Controllers
{
    [Authorize] // will restrict access to anonymous users to all methods in the controller(J.A.)
    public class StandingsController : Controller
    {
        //
        // GET: /Standings/
        private game_entitiesDbContext db = new game_entitiesDbContext();
        private UsersContext db1 = new UsersContext();
        [InitializeSimpleMembership]
        public ActionResult Index()
        {
            standingsViewModel thestandings = new standingsViewModel();

            thestandings.standings = db.leagueStandings.Include(t => t.team).ToList();
            thestandings.gamestats = db.gameStats.ToList();
            thestandings.playerHittingStats = db.playerHittingGameStats.ToList();
            thestandings.playerPitchingStats = db.playerPithichGameStat.ToList();

            ViewBag.defteamname = "Default Team";
          
            return View(thestandings);
        }
        [InitializeSimpleMembership]
        public ActionResult GameDetails(int id = 0)
        {

            standingsViewModel gamedet = new standingsViewModel();
            if (id == 0)
            {
                return RedirectToAction("Index");
            }
            ViewBag.defaulTeamName = "default team";
            gamedet.standings = db.leagueStandings.Include(t => t.team).Include(t => t.team.Pitcher).Include(t => t.team.Catcher).Include(t => t.team.CenterField).Include(t => t.team.Closer).Include(t => t.team.FirstBase).Include(t => t.team.LeftField).Include(t => t.team.Rightfield).Include(t => t.team.SecondBase).Include(t => t.team.ShortStop).Include(t => t.team.ThirdBase).ToList();
            foreach (var team in gamedet.standings)
            {
                if (team.team.teamName == null)
                {
                    team.team.teamName = "default team";
                }

            }
            //gamedet.standings = db.leagueStandings.Include(t => t.team);
            var teamIDs = db.gameStats.Where(t => t.id == id).ToList();
            foreach (var item in teamIDs)
            {
                ViewBag.Homeid= item.HomeTeamID;
                ViewBag.awayid = item.AwayTeamID;
            }

            gamedet.gamestats = db.gameStats.Where(t => t.id == id).ToList();
            gamedet.playerHittingStats = db.playerHittingGameStats.Where(t => t.GameId == id).ToList();
            gamedet.playerPitchingStats = db.playerPithichGameStat.Where(t => t.GameId == id).ToList();
            gamedet.teamPlayers = db.players.ToList();

            return View(gamedet);
        }
    }
}
